package com.hiultra.archive.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.domain.vo.SelectParamsVo;
import com.hiultra.archive.mapper.*;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.service.IArchiveDesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销毁数据Service业务层处理
 *
 * @author paidaxing
 * @date 2024-07-23
 */
@Service
public class ArchiveDesServiceImpl implements IArchiveDesService {

    private static final Logger log = LoggerFactory.getLogger(ArchiveDesTaskServiceImpl.class);
    @Autowired
    private ArchiveDesMapper archiveDesMapper;
    @Autowired
    private RoomArchiveMapper roomArchiveMapper;
    @Autowired
    private ArchiveDesTaskMapper archiveDesTaskMapper;
    @Autowired
    private ArcBoxMapper arcBoxMapper;
    @Autowired
    private ArchiveBoxStatusMapper archiveBoxStatusMapper;

    /**
     * 查询销毁数据
     *
     * @param id 销毁数据主键
     * @return 销毁数据
     */
    @Override
    public ArchiveDes selectArchiveDesById(String id) {
        return archiveDesMapper.selectArchiveDesById(id);
    }

    /**
     * 查询销毁数据列表
     *
     * @param archiveDes 销毁数据
     * @return 销毁数据
     */
    @Override
    public List<ArchiveDes> selectArchiveDesList(ArchiveDes archiveDes) {
        return archiveDesMapper.selectArchiveDesList(archiveDes);
    }

    /**
     * 新增销毁数据
     *
     * @param archiveDes 销毁数据
     * @return 结果
     */
    @Override
    public int insertArchiveDes(ArchiveDes archiveDes) {
        return archiveDesMapper.insertArchiveDes(archiveDes);
    }

    /**
     * 修改销毁数据
     *
     * @param archiveDes 销毁数据
     * @return 结果
     */
    @Override
    public int updateArchiveDes(ArchiveDes archiveDes) {
        return archiveDesMapper.updateArchiveDes(archiveDes);
    }

    /**
     * 批量删除销毁数据
     *
     * @param ids 需要删除的销毁数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDesByIds(String[] ids) {
        return archiveDesMapper.deleteArchiveDesByIds(ids);
    }

    /**
     * 删除销毁数据信息
     *
     * @param id 销毁数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDesById(String id) {
        return archiveDesMapper.deleteArchiveDesById(id);
    }

    /**
     * 修改销毁任务状态
     *
     * @param ids 需要修改的销毁数据主键集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateArchiveDesTaskStatus(String taskId, String[] ids, String localStatus) {
        int i = archiveDesMapper.updateArchiveDesTaskStatus(ids, localStatus);
        //伪删除档案信息
        for (String id : ids) {
            ArchiveDes archiveDes = archiveDesMapper.selectArchiveDesById(id);
            roomArchiveMapper.updateRoomArchiveDelflag(archiveDes.getDesArchiveId());

            //记录档案盒状态
            ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxCode(archiveDes.getBoxNo());
            //判空
            if (ObjectUtil.isNull(arcBox)) {
                log.info("不存在档案盒标识" + archiveDes.getBoxNo() + "，请检查后重试！");
                throw new ServiceException("不存在档案盒标识" + archiveDes.getBoxNo() + "，请检查后重试！");
            }

            ArchiveBoxStatus archiveBoxStatus = new ArchiveBoxStatus();
            archiveBoxStatus.setAreaNo(arcBox.getAreaNo());
            archiveBoxStatus.setCabinetNo(arcBox.getCabinetNo());
            archiveBoxStatus.setBoxBarCode(arcBox.getBoxRfid());
            archiveBoxStatus.setBoxNo(arcBox.getBoxCode());
            archiveBoxStatus.setHouseLocationNo(arcBox.getHouseLocationNo());

            archiveBoxStatus.setBoxStatus(BusinessConstants.ARC_DES);
            archiveBoxStatus.setRecordTime(DateUtils.getNowDate());
            archiveBoxStatus.setPushStatus(BusinessConstants.PUSHSTATUS);
            archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);
        }

        //查询是否都销毁完成
        int desNum = 0;
        String finalTaskId = taskId;

        List<ArchiveDes> archiveDesListAll = archiveDesMapper.selectArchiveDesList(new ArchiveDes() {{
            setTaskId(finalTaskId);
        }});
        for (ArchiveDes archiveDes : archiveDesListAll) {
            if (BusinessConstants.DES_STATUS.equals(archiveDes.getLocalStatus())) {
                desNum++;
                break;
            }
        }
        if (desNum <= 0) {
            ArchiveDesTask archiveDesTask = new ArchiveDesTask();
            archiveDesTask.setTaskId(finalTaskId);
            archiveDesTask.setStatus(BusinessConstants.END_DES_STATUS);
            archiveDesTaskMapper.updateArchiveDesTask(archiveDesTask);
        }

        return i;
    }

    /**
     * 根据任务id查询销毁档案详情
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ArchiveDes> selectArchiveDesByTaskId(String taskId) {
        return archiveDesMapper.selectArchiveDesByTaskId(taskId);
    }

}
